package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.domain.Iplog;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.mapper.AccountMapper;
import com.xmg.p2p.base.mapper.IplogMapper;
import com.xmg.p2p.base.mapper.UserinfoMapper;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.mapper.LogininfoMapper;
import com.xmg.p2p.base.service.ILogininfoService;
import com.xmg.p2p.base.util.MD5;

import java.util.Date;

@Service
public class LogininfoServiceImpl implements ILogininfoService {
	@Autowired
	private LogininfoMapper logininfoMapper;
	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private UserinfoMapper userinfoMapper;
	@Autowired
	private IplogMapper iplogMapper;
	@Override
	public void register(String username, String password,int userType) {
		// 判断用户名是否存在,
		int count = logininfoMapper.getCountByUsername(username,userType);
		if(count<=0) {
			Logininfo logininfo = new Logininfo();
			logininfo.setUsername(username);
			logininfo.setPassword(MD5.encode(password));
			logininfo.setState(Logininfo.STATE_NORMAL);
			logininfo.setUserType(userType);
			this.logininfoMapper.insert(logininfo);

			Account account = new Account();
			account.setId(logininfo.getId());
			this.accountMapper.insert(account);

			Userinfo userinfo = new Userinfo();
			userinfo.setId(logininfo.getId());
			this.userinfoMapper.insert(userinfo);

		}else {

			throw new RuntimeException("用户名已经存在!");
		}
		
	}

	@Override
	public Logininfo get(Long id) {
		return logininfoMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean checkUsername(String username,int userType) {
		return logininfoMapper.getCountByUsername(username,userType)>0;
	}

	@Override
	public Logininfo login(String username, String password,String ip,int userType) {
		Logininfo logininfo = logininfoMapper.login(username,MD5.encode(password), userType);
		Iplog iplog = new Iplog();
		iplog.setIp(ip);
		iplog.setLoginTime(new Date());
		iplog.setUserType(userType);
		if(logininfo==null){
			iplog.setUsername(username);
			iplog.setState(Iplog.STATE_FAILED);
			//throw new RuntimeException("用户名密码错误!");
		}else{
			iplog.setLogininfoId(logininfo.getId());
			iplog.setUsername(logininfo.getUsername());
			iplog.setState(Iplog.STATE_SUCCESS);
			//iplog.setUserType(iplog.getUserType());
            UserContext.putCurrent(logininfo);

        }
        iplogMapper.insert(iplog);
		return logininfo;
	}

	@Override
	public void initAdmin() {
		int count = logininfoMapper.checkCountByUserType(Logininfo.USER_MANAGER);
		if(count==0) {
			Logininfo admin = new Logininfo();
			admin.setUsername("admin");
			admin.setPassword(MD5.encode("1111"));
			admin.setState(Logininfo.STATE_NORMAL);
			admin.setUserType(Logininfo.USER_MANAGER);
			logininfoMapper.insert(admin);
		}
		
	}

}
